package netty.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    private static final int PORT = 8900;

    private static final String HOST = "127.0.0.1";

    public static class ReadMsg extends Thread {

        private Socket socket;

        public ReadMsg(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()))) {
                String msg = null;
                while ((msg = br.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(HOST, PORT);
        new ReadMsg(socket).start();
        System.out.println("请输入请求消息：");
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        while (true) {
            writer.println(new Scanner(System.in).next());
            writer.flush();
        }
    }
}
